import { ArgumentsHost, Catch, ExceptionFilter } from '@nestjs/common'
import moment from 'moment'
import { CustomLoggerService } from '../logger'

@Catch()
export class AnyExceptionFilter implements ExceptionFilter {
    constructor(private readonly logger: CustomLoggerService) {}

    public catch(exception: any, host: ArgumentsHost) {
        const ctx = host.switchToHttp()
        const response = ctx.getResponse()
        const request = ctx.getRequest()
        this.logger.filterLog(host, exception)
        response.status(200).json({
            flag: -1,
            timestamp: moment().format('YYYY-MM-DD HH:mm:ss'),
            path: request.url,
            message: exception.message,
        } as any)
    }
}
